<div class="row">

    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
        <div class="widget am-cf">
            <div class="widget-head am-cf">
                <div class="widget-title am-fl">
                    <#if (addOrUpd!'') == "upd">
                        更新角色
                        <#else >
                        新增角色
                    </#if>
                </div>
                <div class="widget-function am-fr">
                </div>
            </div>
            <div class="widget-body am-fr">

                <form id="formRole" class="am-form tpl-form-line-form">
                    <input type="hidden" id="id" name="id" value="${role.id!''}">

                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">角色名 <span class="tpl-form-line-small-title"></span></label>
                        <div class="am-u-sm-9">
                            <input type="text" class="tpl-form-input" id="name" name="name" value="${role.name!''}" placeholder="请输入角色名">
                            <small>定义的角色名。</small>
                        </div>
                    </div>

                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">角色显示顺序 <span class="tpl-form-line-small-title"></span></label>
                        <div class="am-u-sm-9">
                            <input type="text" class="tpl-form-input" id="sort" value="${role.sort!''}" name="sort" placeholder="请输入权限">
                            <small>角色显示顺序，一般不需要去修改。</small>
                        </div>
                    </div>

                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">状态 <span class="tpl-form-line-small-title"></span></label>
                        <div class="am-u-sm-9">
                            <input <#if ((role.status!'') == '0')>checked </#if> type="radio" class="tpl-form-input" id="status1" name="status" value="0"> 正常
                            <input <#if ((role.status!'') == '1')>checked </#if> type="radio" class="tpl-form-input" id="status2" name="status" value="1"> 禁用
                        </div>
                    </div>

                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">用户拥有权限 <span class="tpl-form-line-small-title"></span></label>
                        <div class="am-u-sm-9">
                            <#if userRoleAuthorList??>
                                <#list userRoleAuthorList as userRoleAuthor>
                                    <#if userRoleAuthor??>
                                        <input checked type="checkbox" name="authorArr" id="authorArr" value="${userRoleAuthor.aid!''}" > ${userRoleAuthor.achboundary!''} <br/>
                                    </#if>
                                </#list>
                            </#if>
                        </div>
                    </div>

                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">备注 <span class="tpl-form-line-small-title"></span></label>
                        <div class="am-u-sm-9">
                            <textarea id="remark" name="remark">${role.remark!''}</textarea>
                        </div>
                    </div>


                    <div class="am-form-group">
                        <div class="am-u-sm-9 am-u-sm-push-3">
                            <#if (addOrUpd!'') == "upd">
                                <button type="button" class="am-btn am-btn-primary tpl-btn-bg-color-success " onclick="updRole()">更新角色</button>
                            <#else >
                                <button type="button" class="am-btn am-btn-primary tpl-btn-bg-color-success " onclick="addRole()">新增角色</button>
                            </#if>
                            <button type="button" class="am-btn am-btn-success " onclick="javascrtpt:window.location.href='/main/indexUrl.php'">返回首页</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>





</div>


<script type="text/javascript">

    /**
     * 更新角色
     */
    function updRole() {
        if (!checkRole()){
            return;
        }

        var id = $("#id").val();
        if (checkNull(id)){
            alert("未找到待更新的角色ID，无法执行更新");
            return;
        }
        var url = "${request.contextPath}/author/addOrUpdRole.php";
        var formRole = $("#formRole").serialize();
        $.ajax({
            url: url,
            type:"post",
            dataType:"json",
            data:formRole,
            success:function(data){
                if (data.status == "success"){
                    alert(data.msg);
                    window.location.href="/author/listRoleViewUrl.php";
                } else if (data.status == 'error'){
                    alert(data.msg);
                } else {
                    alert("未知情况");
                }
                clearForm("formRole");
            },
            error:function(e){
                alert("错误！！");
            }
        });
    }

    /**
     * 添加角色
     */
    function addRole() {
        if (!checkRole()){
            return;
        }

        var url = "${request.contextPath}/author/addOrUpdRole.php";
        var formRole = $("#formRole").serialize();
        $.ajax({
            url: url,
            type:"post",
            dataType:"json",
            data:formRole,
            success:function(data){
                if (data.status == "success"){
                    alert(data.msg);
                    window.location.href="/author/listRoleViewUrl.php";
                } else if (data.status == 'error'){
                    alert(data.msg);
                } else {
                    alert("未知情况");
                }
                clearForm("formRole");
            },
            error:function(e){
                alert("错误！！");
            }
        });
    }

    /**
     * 检查数据是否填录完整
     * 返回 false， 则数据未完整
     * 返回 true， 则数据正常
     */
    function checkRole() {
        var name = $("#name").val();
        if (checkNull(name)){
            alert("请输入角色名");
            return false;
        }

        var status = $('input:radio[name="status"]:checked').val();
        if(status == null){
            alert("请选择 状态");
            return false;
        }
        return true;
    }


</script>